<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>

    <el-form-item label="图像" prop="profilePicture">
      <!-- <el-input v-model="dataForm.profilePicture" placeholder="图像"></el-input> -->
      <single-upload v-model="dataForm.profilePicture"></single-upload>
    </el-form-item>
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="passWord">
      <el-input v-model="dataForm.passWord" placeholder="密码"></el-input>
    </el-form-item>
 
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload";
  export default {
     components: { SingleUpload },
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          backgroundPicture: '',
          createTime: '',
          email: '',
          introduction: '',
          lastModifyTime: '',
          outDate: '',
          passWord: '',
          profilePicture: '',
          userName: '',
          validataCode: ''
        },
        dataRule: {
          backgroundPicture: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          introduction: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          lastModifyTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          outDate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          passWord: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          profilePicture: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          validataCode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/admin/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.backgroundPicture = data.user.backgroundPicture
                this.dataForm.createTime = data.user.createTime
                this.dataForm.email = data.user.email
                this.dataForm.introduction = data.user.introduction
                this.dataForm.lastModifyTime = data.user.lastModifyTime
                this.dataForm.outDate = data.user.outDate
                this.dataForm.passWord = data.user.passWord
                this.dataForm.profilePicture = data.user.profilePicture
                this.dataForm.userName = data.user.userName
                this.dataForm.validataCode = data.user.validataCode
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/admin/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'backgroundPicture': this.dataForm.backgroundPicture,
                'createTime': this.dataForm.createTime,
                'email': this.dataForm.email,
                'introduction': this.dataForm.introduction,
                'lastModifyTime': this.dataForm.lastModifyTime,
                'outDate': this.dataForm.outDate,
                'passWord': this.dataForm.passWord,
                'profilePicture': this.dataForm.profilePicture,
                'userName': this.dataForm.userName,
                'validataCode': this.dataForm.validataCode
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
